import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref()
    // 跳转路由信息
    const routerfile = ref()

    const getProfile = () => {
      const val = uni.getStorageSync('profile')
      profile.value = val
      return val
    }
    // 保存会员信息，登录时使用
    const setProfile = (val: any) => {
      uni.setStorageSync('profile', val)
      profile.value = val
    }
    // 保存跳转路由信息，跳转时使用
    const setRouterfile = (val: any) => {
      routerfile.value = val
    }

    // 清理会员，退出时使用
    const clearProfile = () => {
      profile.value = undefined
      uni.removeStorageSync('profile')
      console.log('11')
      uni.navigateTo({
        url: '/pages/login/login',
      })
    }
    // 清理跳转路由信息，退出时使用
    const clearRouterfile = () => {
      routerfile.value = undefined
    }
    const setDeviceId = (val: any) => {
      uni.setStorageSync('deviceId', val)
    }
    const getDeviceId = () => {
      const val = uni.getStorageSync('deviceId')
      return val
    }

    const setReminderSound = (value: number) => {
      profile.value.reminder_sound = value
      uni.setStorageSync('profile', profile.value)
    }

    // 记得return
    return {
      profile,
      routerfile,
      getProfile,
      setProfile,
      setRouterfile,
      clearProfile,
      clearRouterfile,
      getDeviceId,
      setDeviceId,
      setReminderSound,
    }
  },
  // todo:持久化
  {
    // 小程序配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
